import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Binary5 extends JFrame implements ActionListener {
    private JComboBox<String>[] opCombo;
    private JComboBox<String> OpCombo1;
    private Button eql;
    private TextField[] txt;
    private int n;
    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            int ret = which();
            Integer num1 = Integer.parseInt(txt[0].getText());
            this.txt[1].setText(MyInteger.toString(num1,ret));

            Integer num2 = Integer.parseInt(txt[2].getText());
            this.txt[3].setText(MyInteger.toString(num2,ret));
            num1 = mySwitch(opCombo[0],num1,num2);
            // 获取输入数值和运算符
            for (int i = 1; i < n; i++) {
                num2 = Integer.parseInt(txt[2*i+2].getText());
                this.txt[2*i+3].setText(MyInteger.toString(num1,ret));
                num1 = mySwitch(opCombo[i],num1,num2);
            }
            this.txt[2*n+2].setText(Integer.toString(num1));
            this.txt[2*n+3].setText(MyInteger.toString(num1,ret));
            //txt[2].setText("");
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, "Invalid input", "Error", JOptionPane.ERROR_MESSAGE);
        } catch (ArithmeticException ex) {
            JOptionPane.showMessageDialog(this, "Divide by zero", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    public int which(){
        switch (OpCombo1.getSelectedIndex()){
            case 0: return 2;
            case 1: return 4;
            case 2: return 8;
            case 3: return 16;
        }
        return -1;
    }
    public int mySwitch(JComboBox<String> op, int num1, int num2){
        int result = 0;
        switch (op.getSelectedIndex()){
            case 0:  // 位与
                result = num1 & num2;
                break;
            case 1:  // 位或
                result = num1 | num2;
                break;
            case 2:  // 位异或
                result = num1 ^ num2;
                break;
            default:
                return num1;
        }
        return result;
    }
    public Binary5(int n){
        super("二进制运算");
        this.n = n;
        txt = new TextField[4+2*n];
        opCombo = new JComboBox[n];
        //关闭窗口
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //表格长宽
        this.setSize(600,200);
        //表格的位置
        this.setLocationRelativeTo(null);
        //表格框架
        this.setLayout(null);
        //进制选择
        OpCombo1 = new JComboBox<>(new String[]{"2", "4", "8","16"});
        OpCombo1.setBounds(220,0,40,20);
        this.add(OpCombo1);
        //第一个文本行
        this.txt[0] = new TextField("0",10);
        txt[0].setBounds(50,20,150,25);
        this.add(txt[0]);
        this.txt[1] = new TextField("",10);
        txt[1].setBounds(220,20,250,25);
        this.add(txt[1]);
        // 创建单选按钮显示并选择运算符
        for (int i = 0; i < n; i++) {
            opCombo[i] = new JComboBox<>(new String[]{"&", "|", "^"});
            opCombo[i].setBounds(0,46+i*25,40,25);
            this.add(opCombo[i]);

            this.txt[2+i*2] = new TextField("0",10);
            txt[2+i*2].setBounds(50,46+i*25,150,25);
            this.add(txt[2+i*2]);

            this.txt[3+i*2] = new TextField("",10);
            txt[3+i*2].setBounds(220,46+i*25,250,25);
            this.add(txt[3+i*2]);
        }

        //"="按钮
        this.eql = new Button("=");
        eql.setBounds(0,46+n*25,40,25);
        this.add(this.eql);
        this.eql.addActionListener(this);

        this.txt[2*n+2] = new TextField("0",10);
        txt[2*n+2].setBounds(50,46+n*25,150,25);
        this.add(txt[2*n+2]);

        this.txt[2*n+3] = new TextField("",10);
        txt[2*n+3].setBounds(220,46+n*25,250,25);
        this.add(txt[2*n+3]);

        this.setVisible(true);//可视化
    }
    public static void main(String[] args) {
        new Binary5(5);
    }
}

